package com.guzhen.wallpager;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Build;
import android.os.Build.VERSION;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings.System;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;

import com.fun.funlivewallpaper.AppApplication;

public final class ScreenUtils {
    private ScreenUtils() {
        throw new UnsupportedOperationException("u can\'t instantiate me...");
    }

    public int calculateDistanceByX(View view0) {
        int[] arr_v = new int[2];
        view0.getLocationOnScreen(arr_v);
        return ScreenUtils.getScreenWidth() - arr_v[0];
    }

    public int calculateDistanceByY(View view0) {
        int[] arr_v = new int[2];
        view0.getLocationOnScreen(arr_v);
        return ScreenUtils.getScreenHeight() - arr_v[1];
    }

    public static int getAppScreenHeight() {
        WindowManager windowManager0 = (WindowManager) AppApplication.application.getSystemService("window");
        if(windowManager0 == null) {
            return -1;
        }

        Point point0 = new Point();
        windowManager0.getDefaultDisplay().getSize(point0);
        return point0.y;
    }

    public static int getAppScreenWidth() {
        WindowManager windowManager0 = (WindowManager) AppApplication.application.getSystemService("window");
        if(windowManager0 == null) {
            return -1;
        }

        Point point0 = new Point();
        windowManager0.getDefaultDisplay().getSize(point0);
        return point0.x;
    }

    public static float getScreenDensity() {
        return Resources.getSystem().getDisplayMetrics().density;
    }

    public static int getScreenDensityDpi() {
        return Resources.getSystem().getDisplayMetrics().densityDpi;
    }

    public static int getScreenHeight() {
        WindowManager windowManager0 = (WindowManager) AppApplication.application.getSystemService("window");
        if(windowManager0 == null) {
            return -1;
        }

        Point point0 = new Point();
        if(Build.VERSION.SDK_INT >= 17) {
            windowManager0.getDefaultDisplay().getRealSize(point0);
            return point0.y;
        }

        windowManager0.getDefaultDisplay().getSize(point0);
        return point0.y;
    }

    public static int getScreenRotation(@NonNull Activity activity0) {
        switch(activity0.getWindowManager().getDefaultDisplay().getRotation()) {
            case 1: {
                return 90;
            }
            case 2: {
                return 180;
            }
            case 3: {
                return 270;
            }
            default: {
                return 0;
            }
        }
    }

    public static int getScreenWidth() {
        WindowManager windowManager0 = (WindowManager) AppApplication.application.getSystemService("window");
        if(windowManager0 == null) {
            return -1;
        }

        Point point0 = new Point();
        if(Build.VERSION.SDK_INT >= 17) {
            windowManager0.getDefaultDisplay().getRealSize(point0);
            return point0.x;
        }

        windowManager0.getDefaultDisplay().getSize(point0);
        return point0.x;
    }

    public static float getScreenXDpi() {
        return Resources.getSystem().getDisplayMetrics().xdpi;
    }

    public static float getScreenYDpi() {
        return Resources.getSystem().getDisplayMetrics().ydpi;
    }

    public static int getSleepDuration() {
        try {
            return Settings.System.getInt( AppApplication.application.getContentResolver(), "screen_off_timeout");
        }
        catch(Settings.SettingNotFoundException settings$SettingNotFoundException0) {
            settings$SettingNotFoundException0.printStackTrace();
            return 0xFFFFFF85;
        }
    }

    public int getViewX(View view0) {
        int[] arr_v = new int[2];
        view0.getLocationOnScreen(arr_v);
        return arr_v[0];
    }

    public int getViewY(View view0) {
        int[] arr_v = new int[2];
        view0.getLocationOnScreen(arr_v);
        return arr_v[1];
    }

    public static boolean isFullScreen(@NonNull Activity activity0) {
        return (activity0.getWindow().getAttributes().flags & 0x400) == 0x400;
    }

    public static boolean isLandscape() {
        return  AppApplication.application.getResources().getConfiguration().orientation == 2;
    }

    public static boolean isPortrait() {
        return  AppApplication.application.getResources().getConfiguration().orientation == 1;
    }

    public static boolean isScreenLock() {
        KeyguardManager keyguardManager0 = (KeyguardManager) AppApplication.application.getSystemService("keyguard");
        return keyguardManager0 == null ? false : keyguardManager0.inKeyguardRestrictedInputMode();
    }



    public static void setFullScreen(@NonNull Activity activity0) {
        activity0.getWindow().addFlags(0x400);
    }

    @SuppressLint({"SourceLockedOrientationActivity"})
    public static void setLandscape(@NonNull Activity activity0) {
        activity0.setRequestedOrientation(0);
    }

    public static void setNonFullScreen(@NonNull Activity activity0) {
        activity0.getWindow().clearFlags(0x400);
    }

    @SuppressLint({"SourceLockedOrientationActivity"})
    public static void setPortrait(@NonNull Activity activity0) {
        activity0.setRequestedOrientation(1);
    }

    @RequiresPermission("android.permission.WRITE_SETTINGS")
    public static void setSleepDuration(int v) {
        Settings.System.putInt( AppApplication.application.getContentResolver(), "screen_off_timeout", v);
    }

    public static void toggleFullScreen(@NonNull Activity activity0) {
        boolean z = ScreenUtils.isFullScreen(activity0);
        Window window0 = activity0.getWindow();
        if(z) {
            window0.clearFlags(0x400);
            return;
        }

        window0.addFlags(0x400);
    }
}

